package com.loveyukari.dao.impl;

import com.loveyukari.common.Pager;
import com.loveyukari.dao.BanAuthDao;
import com.loveyukari.model.BanAuth;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Repository
public class JdbcBanAuthDao extends BaseDao implements BanAuthDao {

    private RowMapper<BanAuth> banAuthRowMapper = new RowMapper<BanAuth>() {
        @Override
        public BanAuth mapRow(ResultSet resultSet, int i) throws SQLException {
            BanAuth banAuth = new BanAuth();
            banAuth.setId(resultSet.getInt("id"));
            banAuth.setAuthId(resultSet.getInt("auth_id"));
            banAuth.setAutoStatus(resultSet.getInt("auto_status"));
            banAuth.setStatus(resultSet.getInt("status"));
            banAuth.setMessage(resultSet.getString("message"));
            banAuth.setEndTime(resultSet.getTimestamp("end_time"));
            banAuth.setStartTime(resultSet.getTimestamp("start_time"));
            banAuth.setChatmsg(resultSet.getString("chatmsg"));
            banAuth.setExecutor(resultSet.getInt("executor"));
            return banAuth;
        }
    };
    @Override
    public List<BanAuth> selectAutoShutUp() {
        String sql = "select * from ban_auth where auto_status = 1 and status = 1";
        List<BanAuth> list = this.getJdbcTemplate().query(sql, banAuthRowMapper);
        return list;
    }

    @Override
    public List<BanAuth> selectShutUpList(Pager pager) {
        String sql = "select * from ban_auth where status = 1 order by start_time desc limit ?,?";
        List<BanAuth> list = this.getJdbcTemplate().query(sql, banAuthRowMapper, pager.getOffset(), pager.getSize());
        return list;
    }

    @Override
    public int countShutUp() {
        String sql = "select count(1) from ban_auth where status = 1";
        Integer count = this.getJdbcTemplate().queryForObject(sql, Integer.class);
        return count;
    }

    @Override
    public void insertBanAuth(BanAuth banAuth) {
        String sql = "insert into ban_auth (auth_id,start_time,end_time,auto_status,executor,message,status,chatmsg) values(?,?,?,?,?,?,?,?)";
        this.getJdbcTemplate().update(sql,banAuth.getAuthId(),banAuth.getStartTime(),banAuth.getEndTime(),banAuth.getAutoStatus(),banAuth.getExecutor(),banAuth.getMessage(),banAuth.getStatus(),banAuth.getChatmsg());
    }

    @Override
    public void updateBanAuthAutoStatus(int id, int status) {
        String sql = "update ban_auth set auto_status = ? where id = ?";
        this.getJdbcTemplate().update(sql,status,id);
    }

    @Override
    public void updateBanAuthAutoExpire() {
        String sql = "update ban_auth set status = 0 where end_time < now()";
        this.getJdbcTemplate().update(sql);
    }

    @Override
    public void updateBanAuthExpire(int id) {
        String sql = "update ban_auth set status = 0 where id = ?";
        this.getJdbcTemplate().update(sql,id);
    }


    @Override
    public List<BanAuth> selectShutUpHistory(int authId) {
        String sql = "select * from ban_auth where auth_id = ? order by start_time desc";
        List<BanAuth> list = this.getJdbcTemplate().query(sql, banAuthRowMapper, authId);
        return list;
    }

    @Override
    public BanAuth selectShutUp(int authId) {
        String sql = "select * from ban_auth where auth_id = ? and status = 1 order by start_time desc limit 1";
        List<BanAuth> list = this.getJdbcTemplate().query(sql, banAuthRowMapper, authId);
        if (list.isEmpty()){
            return null;
        }
        return list.get(0);
    }

    @Override
    public void updateLoseAuth(int authId) {
        String sql = "update ban_auth set status = 0 where auth_id = ?";
        this.getJdbcTemplate().update(sql,authId);
    }

    @Override
    public BanAuth selectById(int id) {
        String sql = "select * from ban_auth where id = ?";
        List<BanAuth> list = this.getJdbcTemplate().query(sql, banAuthRowMapper, id);
        if (list.isEmpty()){
            return null;
        }
        return list.get(0);
    }
}
